<template>
  <el-dialog :title="titleMap[mode]" v-model="visible" customClass="customWidth" destroy-on-close
             @closed="$emit('closed')">
    <el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px"
             label-position="left">
      <el-form-item label="设施名称" prop="name">
        <el-input v-model="form.name" placeholder="设施名称" clearable maxlength="180"></el-input>
      </el-form-item>
      <el-form-item label="设施数量" prop="count">
        <el-input v-model="form.count" placeholder="设施数量" clearable maxlength="40"></el-input>
      </el-form-item>
      <el-form-item label="设施单位">
        <el-select v-model="form.unit" placeholder="设施单位">
          <el-option label="部" value="部"/>
          <el-option label="个" value="个"/>
          <el-option label="㎡" value="㎡"/>
          <el-option label="家" value="家"/>
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="visible=false">取 消</el-button>
      <el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">确 定</el-button>
    </template>
  </el-dialog>

</template>
<script>
export default {
  emits: ['success', 'closed'],
  data() {
    return {
      visible: false,
      isSaveing: false,
      isShowMsg: false,
      roomList: [],
      deviceList: [],
      mode: "add",
      titleMap: {
        add: '增加配套设施',
        edit: '编辑配套设施',
        show: '查看配套设施'
      },
      fileList: [],
      apiObj: this.$API.file.attach.uploadFile,
      templates: [],
      form2: {},
      form: {
        name: '',
        count: null,
        unit: '',
        buildingId: 0
      },
      formSpace: {},
      formPackage: {},
      formPS: {},
      rules: {
        notes_title: [
          {required: true, message: '配套设施标题不能为空', trigger: 'blur'}
        ]
      }
    }
  },
  mounted() {
  },
  methods: {
    //显示
    open(mode = 'add') {
      this.mode = mode;
      this.visible = true;
      return this
    },
    submit() {
      this.$refs.dialogForm.validate(async (valid) => {
        if (valid) {
          this.isSaveing = true;
          let param = {building_id: this.form.buildingId, key: this.form.name, value: this.form.count+this.form.unit}
          let res = await this.$API.attractinvestment.building.insertBuildingsupport.post(param);
          this.isSaveing = false;
          if (res.code == 200) {
            this.$emit('success')
            this.visible = false;
            this.$message.success("操作成功")
          } else {
            this.$alert(res.message, "提示", {type: 'error'})
          }
        }
      })
    },
    setCreateData(data, roomList) {
      this.roomList = roomList;
      this.form.buildingId = data.building.id;
    },
  }
}
</script>

<style>
</style>
